import java.util.ArrayList;
import java.util.Scanner ;

public class Customer {
    ShoppingCart cart=new ShoppingCart()  ;
    private ArrayList<Order> orderHistory;
    private int ID;
    private String name;
    private String rank;
    private String date;
    private int sum;
    private int phonenumber;
    private String email;
    private String code;

    public Customer(int ID, String name, String rank, String date, int sum, int phonenumber, String email, String code) {
        this.ID = ID;
        this.name = name;
        this.rank = rank;
        this.date = date;
        this.sum = sum;
        this.phonenumber = phonenumber;
        this.email = email;
        this.code = code;
        this.orderHistory = new ArrayList<>();
    }
    @Override
    public String toString() {
        return  "ID=" + ID +"\t\tname=" + name +"\t\trank=" + rank +"\t\tdate=" + date +"\t\tsum=" + sum +"\t\tphonenumber=" + phonenumber +"\t\temail='" + email;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }
    public int getID() {
        return ID;
    }

    public void setCode(String code) {
        this.code = code;
    }


    public void register(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("***********用户注册************");
        System.out.println("请输入用户名（长度不少于5个字符）：");
        String input = reader.next();
        while (input.length() < 5){
            System.out.println("长度少于5个字符，请重新输入：");
            input = reader.next();
        }
        setName(input);
        System.out.println("请输入密码（长度大于8个字符，包括大小写，数字和标点符号）：");
        for (; ; ) {
            input = reader.next();
            if (input.length() <= 8) {
                System.out.println("长度未大于8个字符，请重新输入：");
                continue;
            }
            //正则表达式判断密码所包含字符
            else if (!input.matches(".*[a-z]+.*")) {
                System.out.println("未含小写字母，请重新输入：");
                continue;
            }
            else if (!input.matches(".*[A-Z]+.*")) {
                    System.out.println("未含大写字母，请重新输入：");
                    continue;
                }
            else if (!input.matches(".*[0-9]+.*")) {
                        System.out.println("未含数字，请重新输入：");
                        continue;
                    }
            else if (!input.matches(".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*")) {
                            System.out.println("未含标点符号，请重新输入：");
                            continue;
                        }
            setCode(input);
            System.out.println(getName() + "，注册成功！");
            break;
        }
            login(customers, products);
    }


    public void login(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("**********用户登录**********");
        System.out.println("请输入用户名：");
        String input = reader.next();
        while (!input.equals(getName())) {
            System.out.println("用户名错误，请重新输入：");
            input = reader.next();
        }
        System.out.println("请输入密码（忘记密码：输入0）：");
        input= reader.next();
        if (input.equals("0")) forgetCode(customers, products);
        for (int i = 0; ; i++) {
            if (i==5) {
                System.out.println("您已连续输错五次密码，账户将被锁定") ;
                break;
            }
            if (!getCode().equals(input)) {
                System.out.println("密码错误，请重新输入：");
                input= reader.next();
            }
            else {
                System.out.println("登录成功");
                CusMenu(customers,products) ;
                break;
            }
        }
    }

    //忘记密码
    public void forgetCode(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader=new Scanner(System.in) ;
        System.out.println("**********重置密码**********") ;
        System.out.println("请输入用户名：") ;
        while (!this.name.equals(reader.next()))
            System.out.println("用户名错误，请重新输入：");
        System.out.println("请输入注册邮箱地址：") ;
        System.out.println("密码重置成功！系统已将新密码发送至您的注册邮箱") ;
        login(customers, products) ;
    }

    //菜单
    public void CusMenu(ArrayList<Customer> customers, ArrayList<Product> products){
        Scanner reader = new Scanner(System.in);
        System.out.println("请选择操作（输入序号）：1.购物管理 2.修改密码 3.退出登录") ;
        switch (reader.nextInt()){
            case 1:
                manageShopping (customers, products);
                break;
            case 2:
                setCusCode(customers, products) ;
                break;
            case 3:
                Start newstart=new Start() ;
                newstart.menu(customers,products) ;
                break;
        }
    }

    public void manageShopping (ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("--------------------购物管理--------------------");
        System.out.println("请选择操作（输入序号）：1.查看商品列表 2.加入购物车 3.从购物车移除 4.修改购物车中的商品数量 5.结账 6.查看购物历史 7.返回菜单");
        switch (reader.nextInt()) {
            case 1:
                listPro(customers, products);
                break;
            case 2:
                addPro(customers, products);
                break;
            case 3:
                deletePro(customers, products);
                break;
            case 4:
                setPro(customers,products) ;
                break;
            case 5:
                checkout(customers,products);
                break;
            case 6:
                OrderHistory(customers,products);
                break;
            case 7:
                returnMenu(customers, products);
                break;
        }
    }

    //查看商品列表
    public void listPro (ArrayList<Customer> customers, ArrayList<Product> products){
        for (Object obj : products)
            System.out.println(obj);
        returnMenu(customers, products);
    }

    //加入购物车
    public void addPro (ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("----------------添加商品----------------");
        String input="y";
        while (input.equals("y")) {
            System.out.println("请输入要加入购物车的商品编号：");
            int addnum = reader.nextInt();
            Product addProduct =products.get(addnum);
            String addName = addProduct.getName();
            float addPrice = addProduct.getSellPrice();
            System.out.println("请输入商品数量：");
            int addnums = reader.nextInt();
            System.out.println("请输入时间：");
            String addTime = reader.next();
            cart.addProduct(new Product(addName,addnum,null,addTime,null,0, addPrice, addnums));
            System.out.println("添加成功！是否继续添加？（y/n）");
            input= reader.next();
        }
        manageShopping(customers, products);
    }

    //从购物车移除
    public void deletePro (ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("----------------删除商品----------------");
        String input = "y";

        while (input.equals("y")) {
            System.out.println("请输入要移除购物车的商品名称：");
            String deleteName = reader.next();

            // 检查商品是否存在
            Product productToRemove = null;
            for (Product product : cart.getProducts()) {
                if (product.getName().equals(deleteName)) {
                    productToRemove = product;
                    break;
                }
            }

            if (productToRemove == null) {
                System.out.println("未找到该商品在购物车中。");
                continue;
            }

            System.out.println("警告：删除后无法恢复，是否继续？(y/n)");
            if (reader.next().equals("n")) {
                System.out.println("已退出");
                manageShopping(customers, products);
                return;
            }

            cart.remove(productToRemove);
            System.out.println("删除成功！是否继续删除？（y/n）");
            input = reader.next();
        }
        manageShopping(customers, products);
    }

    //修改购物车中的商品数量
    public void setPro (ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("----------------修改商品数量----------------");
        String input="y";
        while (input.equals("y")) {
            System.out.println("请输入要修改数量的商品名称：");
            String setName = reader.next();

            // 查找购物车中是否存在该商品
            Product productToUpdate = null;
            for (Product product : cart.getProducts()) {
                if (product.getName().equals(setName)) {
                    productToUpdate = product;
                    break;
                }
            }

            if (productToUpdate == null) {
                System.out.println("未找到该商品在购物车中。");
                continue; // 继续询问另一个商品名称
            }

            System.out.println("请输入要修改后的数量：");
            int setNum = reader.nextInt();

            // 更新商品数量
            productToUpdate.setAmount(setNum);
            System.out.println("修改成功！是否继续修改？（y/n）");
            input = reader.next();
        }
        manageShopping(customers, products);
    }

    //结账
    public void checkout(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        int count = cart.getProducts().size();
        float total = 0;

        // 计算购物车中所有商品的总金额
        for (Product product : cart.getProducts()) {
            total += product.getSellPrice() * product.getAmount();
        }

        System.out.println("当前购物车中有商品 " + count + " 件，金额共 " + total + " 元，是否确定结账？（y/n）");
        if (reader.next().equals("y")) {
            System.out.println("结账成功！欢迎下次惠顾^^");

            // 创建订单时进行深拷贝
            ArrayList<Product> orderProducts = new ArrayList<>();
            for (Product product : cart.getProducts()) {
                orderProducts.add(new Product(product.getName(), product.getNumber() , null, product.getDate() , null, 0, product.getSellPrice(), product.getAmount()));
            }

            Order newOrder = new Order(orderProducts, total);
            addOrder(newOrder);

            // 结账后可以选择清空购物车
            cart.getProducts().clear();
        } else {
            System.out.println("已取消结账。");
        }
        returnMenu(customers, products);
    }

    //修改密码
    public void setCusCode(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("请输入新密码（长度大于8个字符，包括大小写，数字和标点符号）:");
        String input;
        for (; ; ) {
            input = reader.next();
            if (input.length() <= 8) {
                System.out.println("长度未大于8个字符，请重新输入：");
                continue;
            }
            //正则表达式判断密码所包含字符
            else if (!input.matches(".*[a-z]+.*")) {
                System.out.println("未含小写字母，请重新输入：");
                continue;
            } else if (!input.matches(".*[A-Z]+.*")) {
                System.out.println("未含大写字母，请重新输入：");
                continue;
            } else if (!input.matches(".*[0-9]+.*")) {
                System.out.println("未含数字，请重新输入：");
                continue;
            } else if (!input.matches(".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*")) {
                System.out.println("未含标点符号，请重新输入：");
                continue;
            }
            setCode(input);
            System.out.println(getName() + "，修改密码成功！");
            break;
        }
        login(customers,  products);
    }

    // 查看购物历史
    public void OrderHistory(ArrayList<Customer> customers, ArrayList<Product> products) {
        if (orderHistory.isEmpty()) {
            System.out.println("购物历史为空。");
            return;
        }

        System.out.println("购物历史记录：");
        for (int i = 0; i < orderHistory.size(); i++) {
            System.out.println("订单 " + (i + 1) + ":");
            orderHistory.get(i).showOrder(); // 假定 Order 类中有 showOrder 方法
            System.out.println();
        }
        returnMenu(customers, products);
    }

    // 添加订单到购物历史的方法
    public void addOrder(Order order) {
        orderHistory.add(order);
    }


    //返回菜单
    public void returnMenu(ArrayList<Customer> customers, ArrayList<Product> products){
        Scanner reader = new Scanner(System.in);
        System.out.println("输入0返回菜单");
        if (reader.nextInt() == 0) {
            this.CusMenu(customers, products);
        }
    }
    }
